home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9559 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  128 lines

  1. Path: vela.acs.oakland.edu!jggoslin
  2. From: jggoslin@vela.acs.oakland.edu (Monument)
  3. Newsgroups: comp.lang.c
  4. Subject: passing arrays and returning structs
  5. Date: 11 Mar 1996 20:05:28 GMT
  6. Organization: Oakland University, Rochester, Michigan, U.S.A.
  7. Message-ID: <4i2128$69d@news2.acs.oakland.edu>
  8. NNTP-Posting-Host: vela.acs.oakland.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. I have been wracking my brains over this one.  I needed to write a
  12. program to do the transitive closure of a binary relation matrix, no
  13. problems encountered during that.  However, I tried to streamline the
  14. code, by putting the transitive closure algorithm in it's own
  15. function.  When I did this, I tried to pass the initially read in
  16. matrix into the function as an argument, and return a message buffer
  17. to the calling function.
  18.  
  19. I got a mess of errors, included below for your perusal.  If anyone
  20. has any suggestions on how I could get the pointers and returns fixed,
  21. I would sincerely appreciate it.  Post or email, since I read both
  22. regularly.
  23.  
  24. ===client.h===
  25. #include <stdio.h>
  26. #include <sys/errno.h>
  27. #include <sys/ipc.h>
  28. #include <sys/msg.h>
  29. #include <sys/types.h>
  30.  
  31. #define PATH "./server"
  32. #define PROJ 'B'
  33. #define PERMS 0666
  34. #define MAXSZ 200
  35.  
  36. key_t key;
  37. int msgqid;
  38.  
  39. typedef struct my_msgbuf {
  40.     long mtype;
  41.     int pid;
  42.     int size;
  43.     int data[2*MAXSZ];
  44. } Message;
  45. ===client.h===
  46.  
  47. ===client.c===
  48. // header files
  49. #include "client.h"
  50.  
  51. // function prototypes
  52. struct Message transitive_closure(int matrix[]);
  53.  
  54. // MAINLINE
  55. int main(int argc, char *argv[])
  56. {
  57. [I am going to delete a bunch of code here that is irrelevant to the
  58. problem, since I know it works.  All the code I delete does is reads
  59. in the matrix]
  60.  
  61. [here is the relevant call]
  62.     // get the transitive closure
  63.     buffer = transitive_closure(matrix);
  64.  
  65.     // print out results
  66.     puts("Transitive Closure:");
  67.     for (i=0; i<buffer.size; i++)
  68.     {
  69.         for (j=0; j<buffer.size; j++)
  70.             printf(" %d", buffer.data[i*buffer.size+j]);
  71.         printf("\n");
  72.     }
  73.  
  74.     // successful finish
  75.     return 0;
  76. }
  77.  
  78. struct Message* transitive_closure(int matrix[])
  79. {
  80.     int i, j, size;
  81.     Message buffer;
  82.     pid_t pid;
  83.  
  84. [here do I refer to the subtypes in the correct manner?  "." vs. "->"
  85. is what I'm talking about]
  86.  
  87.     buffer.mtype=1L;
  88.     buffer.pid=pid;
  89.     buffer.size=size;
  90.     for (i=0; i<size; i++)
  91.         for (j=0; j<size; j++)
  92.             buffer.data[i*size+j]=matrix[i*size+j];
  93.  
  94. [is the return type correct?]
  95.     return *buffer;
  96. }
  97. ===client.c===
  98.  
  99. ===ERRORS===
  100. cc -g -c client.c
  101. /usr/lib/cmplrs/cc/cfe: Error: client.c, line 58: Functions cannot return a non-object type
  102.      buffer = transitive_closure(matrix);
  103.      ---------------------------^
  104. /usr/lib/cmplrs/cc/cfe: Error: client.c, line 58: Reference an expression of void type or an incomplete type.
  105.      buffer = transitive_closure(matrix);
  106.      ---------------------------^
  107. /usr/lib/cmplrs/cc/cfe: Error: client.c, line 73: redeclaration of 'transitive_closure'; previous declaration at line 11 in file 'client.c'
  108.  struct Message* transitive_closure(int matrix[])
  109.  ----------------^
  110. /usr/lib/cmplrs/cc/cfe: Error: client.c, line 73: Incompatible function return type for this function
  111.  struct Message* transitive_closure(int matrix[])
  112.  ----------------------------------^
  113. /usr/lib/cmplrs/cc/cfe: Error: client.c, line 109: Dereference a non-pointer
  114.      return *buffer;
  115.      --------^
  116. *** Exit 1
  117. Stop.
  118. ===ERRORS===
  119.  
  120.  ----------------------------------------------------------------------------
  121. |          Jeff Goslin - Monument           | "Oh Bentson, you are so        |
  122. |      jggoslin@vela.acs.oakland.edu        |  mercifully free from the      |
  123. |                                           |  ravages of intellect."        |
  124. | http://www.acs.oakland.edu/links/jggoslin |   --Evil, The Time Bandits     |
  125.  ----------------------------------------------------------------------------
  126. |   how come everyone elses religion is a cult but your cult is a religion   |
  127.  ----------------------------------------------------------------------------
  128.